package f.app.al;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.*;

public class FinnanceActivity extends Activity {
	/** Called when the activity is first created. */
	Button bSave, bClear;
	Spinner categorySpinner;
	EditText amountText, dateText, expenseView;
	int year, day, month;
	Date date = new Date();
	String[] expenseList;
	Database budgetDatabase;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    
		bSave = (Button) findViewById(R.id.bSave);
		bClear = (Button) findViewById(R.id.bClear);
		
		expenseList = getResources().getStringArray(R.array.expenseList);
		categorySpinner = (Spinner) findViewById(R.id.categorySpinner);
		dateText = (EditText) findViewById(R.id.dateText);
		expenseView = (EditText) findViewById(R.id.expenseView);
		amountText = (EditText) findViewById(R.id.amountText);
		budgetDatabase = new Database(this);

		year = date.getYear() - 100;
		day = date.getDate();
		month = date.getMonth() + 1;
		dateText.setText(day + "/" + month + "/" + year);

		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.expenseList, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		categorySpinner.setAdapter(adapter);

		bSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Entry transEntry = new Entry();
				transEntry.category = categorySpinner.getSelectedItemPosition();
				transEntry.amount = Integer.parseInt(amountText.getText().toString());
				transEntry.name = expenseView.getText().toString();
				transEntry.year = date.getYear();
				transEntry.month = date.getMonth();
				transEntry.day = date.getDay();
				budgetDatabase.addEntry(transEntry);
				
				amountText.setText("");
				expenseView.setText(null);
				
				
			}
		});

		bClear.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				amountText.setText("");
				expenseView.setText(null);
				//SQLiteDatabase.releaseMemory(); <- wtf is this?
			}
		});
		
	}

}